package com.example.blog.controller;

import com.example.blog.contant.Contants;
import com.example.blog.model.BlogInfo;
import com.example.blog.service.BlogService;
import com.example.blog.utils.JWTUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    //接口只支持get请求
    @GetMapping
    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        log.info("获取博客列表..");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        log.info("getBlogDetail，blogId:{}",blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        //获取token
        String token = request.getHeader(Contants.REQUEST_HEADER_TOKEN);
        //从token中获取id
        Integer userId = JWTUtils.getIdByToken(token);
        if(userId != null && userId == blogDetail.getUserId()){
            blogDetail.setIsLoginUser(true);
        }else{
            blogDetail.setIsLoginUser(false);
        }
        return blogDetail;
    }

    /**
     * 新增博客（json形式）
     * @param blogInfo
     * @return
     */
    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody BlogInfo blogInfo,HttpServletRequest request){
        log.info("添加博客，blogInfo:{}",blogInfo);
        //参数校验
        if(!StringUtils.hasLength(blogInfo.getTitle())
                || !StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        //获取登录用户
        //获取token
        String token = request.getHeader(Contants.REQUEST_HEADER_TOKEN);
        //从token中获取id
        Integer userId = JWTUtils.getIdByToken(token);
        if(userId==null||userId<=0){
            return false;
        }
        blogInfo.setUserId(userId);
        return blogService.insertBlog(blogInfo);
    }

    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo){
        log.info("更新博客，bloginfo:{}",blogInfo);
        //参数校验
        if(blogInfo.getId()==null
                ||!StringUtils.hasLength(blogInfo.getTitle())
                ||!StringUtils.hasLength(blogInfo.getContent())){
           return false;
        }
        blogService.update(blogInfo);
        return true;
    }

    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        log.info("删除博客，blogId{}",blogId);
        if(blogId <= 0){
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        //逻辑删除
        blogInfo.setDeleteFlag(1);
        blogService.update(blogInfo);
        return true;
    }
}
